/*2024.11.13:预处理函数调试完毕
就是bfs函数死了
2024.11.16:bfs函数活了，但是会wa
bfs难写死了，每个元素都得挂一个数组记录，评价是不如dfs
不写了，就这样吧。
2025.2.15：橙题做完了只能练黄题了（悲，用dfs搞掉罢
还是dfs大法好啊*/

#include <iostream>
#include <cstring>
#include <queue>

using namespace std;

#define N 30

string word[N];
int vis[N] = {0};
int f[N][N]; //第i个单词和第j个单词（i连j）相连的最小公共长度
int ans = 0;
int n;

int max(int a, int b){
    if(a >= b)  return a;
    else return b;
}

int pre(int i, int j){  //i连j
    bool tag = true;
    int y = 0;
    for(int x = word[i].size() - 1; x >= 0; x--){
        for(int xx = x; xx < word[i].size(); xx++){
            if(word[i][xx] != word[j][y++]){
                tag =  false;
                break;
            }
        }
        if(tag == true){
            return word[i].size() - x;
        }
        y = 0;
        tag = true;
    }
    return 0;
}

/*void bfs(char st){
    queue<pair<int, int>> q;//单词序号，当前龙长度
    for(int i  = 1; i <= n; i++){
        if(word[i][0] == st){
            q.push(make_pair(i, 0));
        }
    }
    pair<int, int>  tmp;
    while(!q.empty()){
        tmp = q.front();
        q.pop();
        for(int i = 1; i <= n; i++){
            if(tmp.first == i)    continue;
            if(vis[i] >= 2) continue;
            if(f[tmp.first][i] == 0)    continue;
            if(word[i].size() == f[tmp.first][i] || word[tmp.first].size() == f[tmp.first][i])  continue;
            ans = max(ans, tmp.second + word[i].size() - f[tmp.first][i]);
            q.push(make_pair(i, tmp.second + word[i].size() - f[tmp.first][i]));
            vis[i]++;
        }
    }
}*/

void dfs(int x, int len){//尾部单词编号,当前总长度
    vis[x]++;
    bool find = false;
    for(int i = 1; i <= n; i++){
        //if(x == i)  continue;
        if(vis[i] >= 2)  continue;
        if(f[x][i] == 0)    continue;
        if(word[i].size() == f[x][i] || word[x].size() == f[i][x])  continue;
        find = true;
        dfs(i, len + word[i].size() - f[x][i]);
    }
    if(!find){  //没找到，统计答案
        ans = max(len, ans);
    }
    vis[x]--;
    return;
}

int main(){
    char st;
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n;
    for(int i = 1; i<= n; i++){
        cin >> word[i];
    }
    cin >> st;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            f[i][j] = pre(i, j);
        }
    }
    for(int i = 1; i <= n; i++){
        if(word[i][0] == st){
            dfs(i, word[i].size());
        }
    }
    cout << ans;
    return 0;
}